package com.yycx.module.bus.provider.handler.haiying.tasktodo.settle.pay;

import com.yycx.bpm.provider.common.handler.ProcessStartHandler;
import com.yycx.bpm.provider.model.ProcessInstanceRequest;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.msg.provider.websocket.WebSocketServerEndpoint;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 市场结算费用申请流程启动
 *
 * @author zyf
 */
@Component
public class HySettlePayStartProcessHandler implements ProcessStartHandler {

    @Resource
    private BusPaymentService paymentService;
    @Resource
    private WebSocketServerEndpoint socketServerEndpoint;


    /**
     * 初始化流程参数
     *
     * @param processInstanceRequest
     * @return
     */
    @Override
    public String init(ProcessInstanceRequest processInstanceRequest) {
        processInstanceRequest.setProcessKey("process_settle_invoice_flp");
        processInstanceRequest.setTenantId("flp");
        return processInstanceRequest.getBusinessKey();
    }

    /**
     * 设置全局流程变量
     *
     * @param processInstanceRequest
     * @param definition
     * @return
     */
    @Override
    public Map<String, Object> initVariables(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition) {
        Map<String, Object> map = new HashMap<>(5);
        BusPayment payment = paymentService.getById(processInstanceRequest.getBusinessKey());
        if (!payment.getPaymentState().equals(0)) {
            ApiAssert.failure("请勿重复提交");
        }
        String drugType=payment.getDrugType();
        ApiAssert.isNotEmpty("业务类型不能为空",drugType);
        map.put("drugType",payment.getDrugType());
        map.put("paymentNo", payment.getPaymentNo());
        map.put("amountType", payment.getAmountType());
        map.put("companyId", payment.getCompanyId());
        //是否需要开票(如果部门开票选了大恒泽,就不需要二次开票)
        map.put("needKp",CommonConstants.INT_1);
        return map;
    }

    /**
     * 启动完成业务处理
     *
     * @param processInstanceRequest
     * @param definition
     * @param processInstance
     * @param tasks
     * @param variables
     */
    @Override
    public Boolean afterComplete(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition, ProcessInstance processInstance, List<Task> tasks, Map<String, Object> variables) {
        String businessKey = processInstanceRequest.getBusinessKey();
        String processInstanceId = processInstance.getProcessInstanceId();
        //更新结算申请状态为审批中
        return  paymentService.updatePaymentState(Long.parseLong(businessKey), processInstanceId, CommonConstants.INT_1);
        //List<Long> userIds = roleUserMapper.selectUserIdByRoleCode("ROLE_NQBZ", Long.parseLong(companyId.toString()));
        //socketServerEndpoint.sendMessage(2L, "订单审核", "您有新的订单待审核,单号:" + orderNo, userIds, 2);
    }
}
